package com.example.weather;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MoreActivity extends AppCompatActivity {
    RadioGroup exbgRg;
    private SharedPreferences pref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        exbgRg = findViewById(R.id.more_rg);
        pref = getSharedPreferences("bg_pref", MODE_PRIVATE);
        setRGListener();
    }
    public void shareSoftware(View view){
        Intent intent=new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT,"下载地址为");
        startActivity(Intent.createChooser(intent,"天气预报"));
    }
    public void click_more(View view){
        shareSoftware(view);
    }
    public void Send_mss(View view){
        Intent intent=new Intent(this,MsgActivity.class);
        startActivity(intent);
    }
    public void click_back(View view){
        Intent intent=new Intent(this,MainActivity.class);
        startActivity(intent);
    }
    public void exchange_bg(View view){
        if (exbgRg.getVisibility() == View.VISIBLE) {
            exbgRg.setVisibility(View.GONE);
        }else{
            exbgRg.setVisibility(View.VISIBLE);
        }
    }
    private void setRGListener() {
        /* 设置改变背景图片的单选按钮的监听*/
        exbgRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                获取目前的默认壁纸
                int bg = pref.getInt("bg", 0);
                SharedPreferences.Editor editor = pref.edit();
                Intent intent = new Intent(MoreActivity.this,MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                switch (checkedId) {
                    case R.id.more_rb_green:
                        if (bg==0) {
                            Toast.makeText(MoreActivity.this,"您选择的为当前背景，无需改变！",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        editor.putInt("bg",0);
                        editor.commit();
                        System.out.println("用户更改为青瓦白墙壁纸");
                        break;
                    case R.id.more_rb_pink:
                        if (bg==1) {
                            Toast.makeText(MoreActivity.this,"您选择的为当前背景，无需改变！",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        editor.putInt("bg",1);
                        editor.commit();
                        System.out.println("用户更改为粉状玉砌壁纸");
                        break;
                    case R.id.more_rb_blue:
                        if (bg==2) {
                            Toast.makeText(MoreActivity.this,"您选择的为当前背景，无需改变！",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        editor.putInt("bg",2);
                        editor.commit();
                        System.out.println("用户更改为蓝色梦想壁纸");
                        break;
                }
                startActivity(intent);
            }
        });

    }
}